package com.self.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.self.servicebase.handler.MyException;
import com.self.serviceedu.entity.EduChapter;
import com.self.serviceedu.entity.EduVideo;
import com.self.serviceedu.entity.chapter.ChapterVo;
import com.self.serviceedu.entity.chapter.VideoVo;
import com.self.serviceedu.mapper.EduChapterMapper;
import com.self.serviceedu.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.self.serviceedu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author xiaozeng
 * @since 2020-09-16
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    //根据课程ID查询章节小节
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //1. 根据课程ID查询出课程所有章节
        QueryWrapper<EduChapter> wapperChapter = new QueryWrapper<>();
        wapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wapperChapter);

        //2. 根据课程Id查询出课程所有小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> eduVideoList = eduVideoService.list(wrapperVideo);

        ArrayList<ChapterVo> finalList = new ArrayList<>();

        //3. 遍历章节list并进行封装。
        for (EduChapter eduChapter : eduChapterList) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //放入最终的List中
            finalList.add(chapterVo);

            ArrayList<VideoVo> finalVideoList = new ArrayList<>();
            //4. 遍历小节list并进行封装。
            for (EduVideo eduVideo : eduVideoList) {
                //判断小节里面的chapterId和章节里的ID是否一致
                if(eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    finalVideoList.add(videoVo);
                }
            }

            //将封装之后的小节list集合，放到章节对象里面
            chapterVo.setChildren(finalVideoList);
        }

        return finalList;
    }

    //删除章节
    @Override
    public boolean deleteChapter(String chapterId) {
        //查询小节,如果章节下边有小节，则不让删除。
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(wrapper);
        if(count > 0){
            //不进行删除
            throw new MyException(20001,"不能删除");
        }else {
            //进行删除
            int result = baseMapper.deleteById(chapterId);
            return result > 0;
        }



    }

    //根据课程Id删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
